import axios from "axios";
import { getToken } from "./auth";

const instance = axios.create({
  baseURL: "http://localhost:3000",
  timeout: 5000,
});
// Add a request interceptor
//全局请求拦截，发送请求之前执行
instance.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    config.headers["authorization"] = "Bearer" + getToken();
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
//请求返回之后执行
instance.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  }
);

/**
 * get请求——查看
 * @param {*} url   请求地址
 * @param {*} params   url参数
 * @returns
 */
export function get(url, params) {
  return instance.get(url, {
    params,
  });
}
/**
 * post请求——创建
 * @param {*} url   请求地址
 * @param {*} data   数据
 * @returns
 */
export function post(url, data) {
  return instance.post(url, data);
}
/**
 * put请求——更新或新增
 * @param {*} url   请求地址
 * @param {*} data   数据
 * @returns
 */
export function put(url, data) {
  return instance.put(url, data);
}
/**
 * delete请求——删除
 * @param {*} url   请求地址
 * @returns
 */
export function del(url) {
  return instance.delete(url);
}
